﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AliveCMS.Models;
using Lucene.Net.Index;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using AliveCMS.App_Code;
namespace AliveCMS.Controllers
{
    public class SiteDataController : ApiController
    {
        // GET api/sitedata
        public IEnumerable<SiteViewModel> Get()
        {
            return MvcApplication.Sites.Select(n => n.Value).ToList();
        }

        // GET api/sitedata/5
        public SiteViewModel Get(Guid id)
        {
            return MvcApplication.Sites[id];
        }

        // POST api/sitedata
        public void Post([FromBody]SiteViewModel value)
        {
            Document doc = new Document();
            value.SiteId = Guid.NewGuid();

            MvcApplication.Sites.Add(value.SiteId, value);

            using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
            {
                doc.Add(new Field("ObjectName", "SiteViewModel", Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("SiteId", value.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Name", value.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Domains", string.Join(",", value.Domains), Field.Store.YES, Field.Index.NOT_ANALYZED));
                indexWriter.AddDocument(doc);
                indexWriter.Optimize();
            }
        }

        // PUT api/sitedata/5
        public void Put(Guid id, [FromBody]SiteViewModel value)
        {
            Document doc = new Document();
            Logger.LogChange<SiteViewModel>(new ChangeViewModel<SiteViewModel>() { Current = MvcApplication.Sites[id], Updated = value });
            MvcApplication.Sites[id] = value;

            using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
            {
                doc.Add(new Field("ObjectName", "SiteViewModel", Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("SiteId", value.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Name", value.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Domains", string.Join(",", value.Domains), Field.Store.YES, Field.Index.NOT_ANALYZED));
                indexWriter.UpdateDocument(new Term("SiteId", value.SiteId.ToString()), doc);
                indexWriter.Optimize();
            }




        }

        // DELETE api/sitedata/5
        public void Delete(Guid id)
        {
            MvcApplication.Sites.Remove(id);
            using (var indexWriter = new IndexWriter(MvcApplication.SiteData, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
            {
                indexWriter.DeleteDocuments(new Term("SiteId", id.ToString()));
            }

        }
    }
}
